package com.willian.andrappfw.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class PositionUtils {

    public PositionUtils() {
    }

    public static Location getLastKnownLocation(Context context) {
        if (ActivityCompat.checkSelfPermission(context, "android.permission.ACCESS_FINE_LOCATION") != 0 && ActivityCompat.checkSelfPermission(context, "android.permission.ACCESS_COARSE_LOCATION") != 0) {
            return null;
        } else {
            @SuppressLint("WrongConstant") LocationManager mLocationManager = (LocationManager)context.getSystemService("location");
            List<String> providers = mLocationManager.getProviders(true);
            Location bestLocation = null;
            Iterator var4 = providers.iterator();

            while(true) {
                Location l;
                do {
                    do {
                        if (!var4.hasNext()) {
                            return bestLocation;
                        }

                        String provider = (String)var4.next();
                        l = mLocationManager.getLastKnownLocation(provider);
                    } while(l == null);
                } while(bestLocation != null && l.getAccuracy() >= bestLocation.getAccuracy());

                bestLocation = l;
            }
        }
    }

    public static void getAddress(Context context, double latitude, double longitude, final PositionUtils.OnGetAddressListener listener) {
        Observable.<String>create((emitter) -> {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            List places = null;

            try {
                places = geocoder.getFromLocation(latitude, longitude, 1);
            } catch (Exception var11) {
                var11.printStackTrace();
                emitter.onError(var11);
            }

            if (places != null && places.size() > 0 && places.get(0) != null) {
                Address address = (Address)places.get(0);

                for(int i = 0; i <= address.getMaxAddressLineIndex(); ++i) {
                    String addressName = address.getAddressLine(i);
                    if (!TextUtils.isEmpty(addressName)) {
                        emitter.onNext(addressName);
                        return;
                    }
                }
            }

            emitter.onNext("");
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
            public void onSubscribe(Disposable d) {
            }

            public void onNext(String address) {
                if (listener != null) {
                    listener.onGetAddress(address);
                }
            }

            public void onError(Throwable e) {
                if (listener != null) {
                    listener.onError(e);
                }
            }

            public void onComplete() {
            }
        });
    }

    @SuppressLint("WrongConstant")
    public static void getCurrentLocation(Context context, LocationListener locationListener, String noProviderWarning) {
        if (locationListener != null && context != null) {
            Location location = getLastKnownLocation(context);
            if (location != null) {
                locationListener.onLocationChanged(location);
            }

            LocationManager locationManager = (LocationManager)context.getSystemService("location");
            if (locationManager != null) {
                if (PermissionChecker.checkSelfPermission(context, "android.permission.ACCESS_FINE_LOCATION") == 0 || PermissionChecker.checkSelfPermission(context, "android.permission.ACCESS_COARSE_LOCATION") == 0) {
                    if (!locationManager.isProviderEnabled("gps") && !locationManager.isProviderEnabled("network")) {
                        Toast.makeText(context, noProviderWarning, 0).show();
                    }

                    locationManager.requestSingleUpdate("gps", locationListener, (Looper)null);
                    locationManager.requestSingleUpdate("network", locationListener, (Looper)null);
                    locationManager.requestSingleUpdate("passive", locationListener, (Looper)null);
                }
            }
        }
    }

    @SuppressLint("WrongConstant")
    public static void onProviderEnabled(Context context, String provider, LocationListener locationListener) {
        LocationManager locationManager = (LocationManager)context.getSystemService("location");
        if (locationManager != null) {
            if (PermissionChecker.checkSelfPermission(context, "android.permission.ACCESS_FINE_LOCATION") == 0 || PermissionChecker.checkSelfPermission(context, "android.permission.ACCESS_COARSE_LOCATION") == 0) {
                locationManager.requestSingleUpdate(provider, locationListener, (Looper)null);
            }
        }
    }

    public static boolean isLocationsEqual(Location l1, Location l2) {
        if (l1 != null && l2 != null) {
            return l1.getLatitude() == l2.getLatitude() && l1.getLongitude() == l2.getLongitude();
        } else {
            return false;
        }
    }

    public interface OnGetAddressListener {
        void onGetAddress(String var1);

        void onError(Throwable var1);
    }
}

